﻿using Kz.Telegram.Bot.Console.Demo;
using Kz.Telegram.Bot.Console.HandleUpdate.Enums;
using Kz.Telegram.Bot.Console.HandleUpdate.Models;
using System.Text;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;

namespace Kz.Telegram.Bot.Console.HandleUpdate
{
    public class SearchOfSupergroupHandler : IHandler
    {
        public async Task Handle(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
        {
            var msg = update.Message!;
            var type = msg.Type;

            if (msg.Text == "测试搜索")
            {
                var pageIndex = 1;
                var adv = SearchResultList.GetOneAdv();
                var res = SearchResultList.GetPageResult(out var PageCount, pageIndex);

                StringBuilder sb = new();
                sb.AppendLine($"[广告] <a href='{adv.Url}'>{adv.Title}</a>");
                sb.AppendLine();

                for (int i = 0; i < res.Count; i++)
                {
                    sb.AppendLine($"{(i + 1) * pageIndex}.<a href='{res[i].Url}'>{res[i].Title} - {res[i].Total}</a>");
                }
                sb.AppendLine();
                sb.AppendLine("提示：点赞或举报，请点下面数字 👇");

                InlineKeyboardMarkup buttons = new[] {

                    CallbackQueryHandler.InlineSearchFilterButtons,
                    [
                        InlineKeyboardButton.WithUrl("😁 购买广告", $"https://t.me/{Bot.UserName}"),
                        InlineKeyboardButton.WithCallbackData("下一页",CallbackQueryModel.Create(InlineCallbackEnum.SearchFilpPage,"2"))
                    ]
                };

                await botClient.SendMessage(msg.Chat.Id, sb.ToString(),
                    parseMode: ParseMode.Html,
                    //replyToMessageId: msg.MessageId,
                    replyMarkup: buttons,
                    linkPreviewOptions: new LinkPreviewOptions() { IsDisabled = true },
                    //disableWebPagePreview: true,
                    protectContent: true,
                    cancellationToken: cancellationToken
                    );
                sb.Clear();
            }
        }
    }
}
